<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/9/25
 * Time: 14:09
 */

namespace app\admin\controller;

use app\admin\model\Food;
use app\common\model\HotelModel;
use think\Request;
use think\Db;
use think\Response;
use app\common\model\GuideModel;
use app\common\model\FoodModel;
use app\common\model\TourModel;
use app\admin\model\Guide as Guide;
use app\common\model\TeamModel;
use think\Session;
use app\hotel\model\Album as AlbumModel;
use think\Queue;

class Merchant extends Common
{
    /***********************************************店铺相关**********************************************/
    /*
     * 美食店铺
     */
    public function food()
    {
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }

            $where['is_del'] = array('eq', 0);
            $where['is_pass'] = array('eq', 1);
            if (isset($pagedata['food_store_name']) && $pagedata['food_store_name'] != '') {
                $where['food_store_name'] = array('like', '%' . $pagedata['food_store_name'] . '%');
            }
            if (isset($pagedata['food_store_tel']) && $pagedata['food_store_tel'] != '') {
                $where['food_store_tel'] = array('like', '%' . $pagedata['food_store_tel'] . '%');
            }
            if (isset($pagedata['food_store_address']) && $pagedata['food_store_address'] != '') {
                $where['food_store_address'] = array('like', '%' . $pagedata['food_store_address'] . '%');
            }
            if (isset($pagedata['examine_time']) && $pagedata['examine_time'] != ''){
                $created_at=explode(' - ', $pagedata['examine_time']); 
                if (isset($created_at) && is_array($created_at)){
                    foreach($created_at as $k=>$v)
                    {
                       $created_at[$k] = strtotime($v);
                    }  
                }
              $where['examine_time']=['between time',$created_at]; 
            }
            $food = new FoodModel();
            $data = $food->getFoodStoreList($where, $startrow, $pagesize);
            $count = $food->getFoodStoreCount($where);
            ajax_return(config('code.success_show'), '请求成功', $data, $count);
        } else {
            //查询所有的店铺
            $res = Db::name('food_store')->where('is_pass', 1)->select();
            $this->assign('res', $res);
            return view();
        }
    }

    /*
     * 查看图片
     */
    public function foodImage()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $data = Db::name('food_store_img')->field('img_path')->where('store_id', $post['id'])->select();

            if ($data) {
                foreach ($data as $key => $v) {
                    $res [] = $v;
                }
                return json(['code' => 0, 'data' => $res]);

            } else {
                return ['code' => 1, 'message' => '暂无图片'];
            }
        }
    }

    /*
     *美食店铺详情
     */
    public function foodInfoDetail()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $where = "food_store_id= {$post['food_store_id']}";

            $food = new foodModel();
            $foodInfo = $food->getFoodStoreList($where, '', '');
            //处理店铺类型
            $type = explode(',', $foodInfo['food_store_tag']);
            $foodInfo['food_store_tag'] = [];
            foreach ($type as $k => $v) {
                if ($v == 1) {
                    array_push($foodInfo['food_store_tag'], '精选美食');
                }
                if ($v == 2) {
                    array_push($foodInfo['food_store_tag'], ' 特色美食');
                }
                if ($v == 3) {
                    array_push($foodInfo['food_store_tag'], ' 快餐简餐');
                }
                if ($v == 4) {
                    array_push($foodInfo['food_store_tag'], ' 主题餐厅');
                }
                if ($v == 5) {
                    array_push($foodInfo['food_store_tag'], ' 酒吧茶馆');
                }
            }

            if ($foodInfo) {
                ajax_return(config('code.success_show'), '请求成功', $foodInfo);
            } else {
                ajax_return(config('code.error_show'), '请求失败');
            }
        }
    }

    /**
     *修改美食店铺基本信息
     */
    public function editFood()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $post = $post['data'];

            if (empty($post['food_store_name']) || empty($post['food_store_tel'])) {
                ajax_return(1, '内容不能为空');
            }
            $where = "food_store_id = {$post['food_store_id']}";
            $data = [
                'food_store_name' => $post['food_store_name'],
                'food_store_tel' => $post['food_store_tel'],
                'send_tel' => $post['send_tel'],
                'city' => $post['province'] . $post['city'] . $post['district'],
                'food_store_address' => $post['address'],
                'food_people_money' => $post['food_people_money'],
                'food_store_tag' => $post['food_store_tag'],
                'food_tag' => $post['food_tag'],
                'food_store_cover' => $post['food_store_cover'],
                'food_store_time' => $post['team_start_time'] . '~' . $post['team_end_time'],

            ];

            $food = new FoodModel();
            $res = $food->edit($where, $data);
            if ($res) {
                $add = $post['province'] . $post['city'] . $post['district'] . $post['address'];
                //添加经纬度

                $data = [
                    'food_store_longitude' => getDoc($add)[0],
                    'food_store_latitude' => getDoc($add)[1]
                ];
                Db::name('food_store')->where('food_store_id', $post['food_store_id'])->update($data);
                ajax_return(0, '修改成功');
            } else {
                ajax_return(2, '修改失败');
            }
        } else {
            //查询需要的数据
            $food_store_id = input('id', '', 'trim');
            $where = "food_store_id = {$food_store_id}";
            $filed = 'food_store_name,food_store_tel,food_store_address,send_tel,food_store_cover,
            food_store_tag,food_people_money,food_store_time,food_tag';
            $food = new FoodModel();
            $res = $food->getFoodStoreList($where, '', '', $filed);
            if (!empty($res['food_store_time'])) {
                $start = explode('~', $res['food_store_time'])[0];
                $end = explode('~', $res['food_store_time'])[1];
            } else {
                $start = '';
                $end = '';
            }
            $this->assign('res', $res);
            $this->assign('start', $start);
            $this->assign('end', $end);
            $this->assign('food_store_id', $food_store_id);
            return $this->fetch();
        }

    }

    /*
   * 美食店铺删除 伪删除
   */
    public function delFood()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $where = "food_store_id = {$post['id']}";
            $food = new Food();
            $res = $food->foodDel($where);
            if ($res) {
                ajax_return(0, '删除成功');
            } else {
                ajax_return(1, '删除失败');
            }
        }
    }

    /***********************************************景点相关**********************************************/

    /*
    * 景点列表
    */
    public function view()
    {
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }

            $where['a.is_del'] = array('eq', 0);
            $where['is_pass'] = array('eq', 1);
            if (isset($pagedata['tour_name']) && $pagedata['tour_name'] != '') {
                $where['tour_name'] = array('like', '%' . $pagedata['tour_name'] . '%');
            }
            if (isset($pagedata['tour_tel']) && $pagedata['tour_tel'] != '') {
                $where['tour_tel'] = array('like', '%' . $pagedata['tour_tel'] . '%');
            }
            //处理好评
            if (isset($pagedata['appraise']) && $pagedata['appraise'] != '') {
                //判断有没有
                $join = explode('~', $pagedata['appraise']);
                if (count($join) == 1) {
                    $where['evaluat_rate'] = $join[0];
                } else if (count($join) == 2) {
                    $where['evaluat_rate'] = ['between', [$join[0], $join[1]]];
                }
            }
            if (isset($pagedata['examine_time']) && $pagedata['examine_time'] != ''){
                $created_at=explode(' - ', $pagedata['examine_time']); 
                if (isset($created_at) && is_array($created_at)){
                    foreach($created_at as $k=>$v)
                    {
                       $created_at[$k] = strtotime($v);
                    }  
                }
              $where['rz_time']=['between time',$created_at]; 
            }
            $tour = new TourModel();
            $data = $tour->getTourList($where, $startrow, $pagesize);
            $count = $tour->getTourCount($where);

            ajax_return(config('code.success_show'), '请求成功', $data, $count);
        } else {
            $res = Db::name('tours')->alias('a')
                ->field('a.*,b.t_admin_id,b.t_admin_tel,b.pid,b.add_time')
                ->join('tour_admin b', 'a.tour_id=b.tour_id', "LEFT")
                ->where('a.is_del', 0)
                ->where('is_pass', 1)
                ->where('b.pid', 0)
                ->select();
            $this->assign('res', $res);
            $res1 = Db::name('tours')->where('is_pass', 1)->where('is_del', 0)->order('tour_id', 'desc')->select();
            foreach ($res1 as $k => $v) {
                $evaluat_rate = model('AppraiseModel')->getAppRate(1, $v['tour_id']);
                Db::name('tours')->where('tour_id', $v['tour_id'])->setField('evaluat_rate', $evaluat_rate);
            }
            return view();
        }
    }

    /**
     * 景点图片
     */
    public function viewImage()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $data = Db::name('tour_img')->field('img_path')->where('tour_id', $post['id'])->select();

            if ($data) {
                foreach ($data as $key => $v) {
                    $res [] = $v;
                }
                return json(['code' => 0, 'data' => $res]);

            } else {
                return ['code' => 1, 'message' => '暂无图片'];
            }
        }

    }

    /*
     *景点的详细信息
     */
    public function viewInfoDetail()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $where = "tour_id= {$post['tour_id']}";

            $food = new TourModel();
            $foodInfo = $food->getTourList($where, '', '');
            //处理店铺类型
            $type = explode(',', $foodInfo['type']);
            $foodInfo['type'] = [];
            foreach ($type as $k => $v) {
                if ($v == 1) {
                    array_push($foodInfo['type'], '精选景点');
                }
                if ($v == 2) {
                    array_push($foodInfo['type'], ' 人文景玩');
                }
                if ($v == 3) {
                    array_push($foodInfo['type'], ' 自然景观');
                }
                if ($v == 4) {
                    array_push($foodInfo['type'], ' 温泉度假');
                }
                if ($v == 5) {
                    array_push($foodInfo['type'], ' 亲子游乐');
                }
            }

            if ($foodInfo) {
                ajax_return(config('code.success_show'), '请求成功', $foodInfo);
            } else {
                ajax_return(config('code.error_show'), '请求失败');
            }
        }
    }

    /*
     *  景点删除 伪删除
     */
    public function delView()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $where = "tour_id = {$post['id']}";
            $view = new TourModel();
            $res = $view->viewDel($where);
            if ($res) {
                ajax_return(0, '删除成功');
            } else {
                ajax_return(1, '删除失败');
            }
        }
    }

    /*
    * 添加景点入住页面展示
    */
    public function viewAdd()
    {
        return $this->fetch();
    }

    /*
     * 添加景点入住ajax
     */
    public function addView()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $post = $post['data'];

            if (!$post['guide_card_img1']) {
                return ['code' => 1, 'message' => '请上传身份证正面照片'];
                exit;
            }
            if (!$post['guide_card_img2']) {
                return ['code' => 1, 'message' => '请上传身份证反面照片'];
            }
            if (!$post['guide_pic']) {
                return ['code' => 1, 'message' => '上传营业执照'];
            }

            if (!$post['live_action']) {
                return ['code' => 1, 'message' => '上传景点实景图'];
            }

            if(!$post['m_image']){
                return ['code' => 1, 'message' => '请上传景点门头照'];
            }
            if(!$post['b_image']){
                return ['code' => 1, 'message' => '请上传景点吧台照'];
            }

            //认证成功，更新客户表
            $post['is_pass'] = 1;
            $post['commission_rate'] = 20;
            $post['create_time'] = date('Y-m-d H:i:s');
            $post['rz_time'] = date('Y-m-d H:i:s');
            $post['last_settlement_date'] = date('Y-m-d',time());
            //处理图片
            $post['fs_lagal'] = $post['guide_card_img1'] . ',' . $post['guide_card_img2'];
            $post['business_license'] = $post['guide_pic'];
            $post['address'] = $post['dizhi'];
            $post['city'] = $post['province'] . $post['city'] . $post['county'];
            //处理经纬度
            $post['longitude'] = getDoc($post['city'].$post['address'])[0];
            $post['latitude'] = getDoc($post['city'].$post['address'])[1];
            unset($post['file']);
            unset($post['province']);

            unset($post['county']);
            unset($post['dizhi']);
            unset($post['guide_card_img1']);
            unset($post['guide_card_img2']);
            unset($post['guide_pic']);
            //先查询是否入住过了
            $res = Db::name('tours')->where('send_tel', $post['send_tel'])
                ->find();
            if ($res) {
                return ['code' => 1, 'message' => '已经申请入住'];
            }
            $admin = Db::name('tour_admin')->where('t_admin_tel',$post['send_tel'])->find();
            if($admin){
                return ['code' => 1, 'message' => '该手机号已经入住商户'];
            }
            //生成账号和密码发送给商户
            $password = substr(base_convert(md5(uniqid(md5(microtime(true)), true)), 16, 10), 0, 6);
            //加密方式
            $hasher = new \PasswordHash(8, false);
            //将密码进行哈希，结果是一个长度为60个字符的字符串
            $hashPassword = $hasher->HashPassword($password);

            $view = new TourModel();
            $res = $view->insertView($post);
            //插入到商户表
            $data = [
                't_admin_tel' => $post['send_tel'],
                'password' => $hashPassword,
                'admin_rename' => $post['tour_name'],
                'tour_id' => $res,
                'add_time' => date('Y-m-d H:i:s')
            ];
            $fasong = [
                '1' => $post['tour_name'],
                '2' => $post['send_tel'],
                '3' => $password,
                '5' => $post['tour_name']
            ];
            if ($res) {
                sendJsmsMessage($post['send_tel'], 176237, $fasong);
                $send = Db::name('tour_admin')->insert($data);
                if ($send) {
                    ajax_return(config('code.success_show'), '添加成功');
                }
            } else {
                ajax_return(2, '添加失败');
            }

        }
    }

    /**
     *修改导游基本信息
     */
    public function editView()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $post = $post['data'];
            if (empty($post['tour_name']) || empty($post['tour_tel'])) {
                ajax_return(1, '内容不能为空');
            }
            $where = "tour_id = {$post['tour_id']}";
            $data = [
                'tour_name' => $post['tour_name'],
                'tour_tel' => $post['tour_tel'],
                // 'send_tel'=>$post['send_tel'],
                'city' => $post['province'] . $post['city'] . $post['district'],
                'address' => $post['address'],
                'type' => $post['type'],
                'tour_tag' => $post['tour_tag'],
                'tour_time' => $post['team_start_time'] . '~' . $post['team_end_time'],
                'tour_covers' => $post['tour_covers']
            ];
            //先去查询原始数据
            // $re = Db::name('tours')->where('tour_id',$post['tour_id'])->find();
            // if($re){

            //     //判断是否有手机号存在
            //     $phone = Db::name('tours')->where('send_tel',$post['send_tel'])->find();
            //     if($phone){
            //         ajax_return(3, '手机号已经存在');
            //         exit;
            //     }
            //     $cun =  Db::name('tour_admin')->where('t_admin_tel',$post['send_tel'])->find();
            //     if($cun){
            //         ajax_return(3, '手机号已经存在');
            //         exit;
            //     }
            // }
            $guide = new TourModel();
            $res = $guide->edit($where, $data);
            if ($res) {
                // if(!empty($re)){
                //     //修改后台的
                //     $admin = Db::name('tour_admin')->where('t_admin_tel',$re['send_tel'])->setField('t_admin_tel',$post['send_tel']);
                //     if($admin){
                //         $fasong = [
                //             '1' => $post['send_tel'],
                //             '2'=>$post['tour_name'],
                //         ];
                //         sendJsmsMessage($post['send_tel'], 176411, $fasong);
                //     }
                // }
                $add = $post['province'] . $post['city'] . $post['district'] . $post['address'];
                //添加经纬度

                $data = [
                    'longitude' => getDoc($add)[0],
                    'latitude' => getDoc($add)[1]
                ];
                Db::name('tours')->where('tour_id', $post['tour_id'])->update($data);
                ajax_return(0, '修改成功');
            } else {
                ajax_return(2, '修改失败');
            }
        } else {
            //查询需要的数据
            $tour_id = input('id', '', 'trim');
            $where = "a.tour_id = {$tour_id}";
            $res = Db::name('tours')->alias('a')
                ->field('a.*')
                ->join('tour_admin b', 'a.tour_id=b.tour_id', "LEFT")
                ->where($where)
                ->where('b.pid', 0)
                ->find();
            if (!empty($res['tour_time'])) {
                $start = explode('~', $res['tour_time'])[0];
                $end = explode('~', $res['tour_time'])[1];
            } else {
                $start = '';
                $end = '';
            }
            $this->assign('res', $res);
            $this->assign('start', $start);
            $this->assign('end', $end);
            $this->assign('tour_id', $tour_id);
            return $this->fetch();
        }

    }

    /*
     * 修改景点商户密码
     */
    public function editPass()
    {
        if (Request::instance()->isAjax()) {
            $data = Request::instance()->post();
            $data = $data['data'];
            //加密方式
            $hasher = new \PasswordHash(8, false);
            //将密码进行哈希，结果是一个长度为60个字符的字符串
            $hashPassword = $hasher->HashPassword($data['password']);
            $where = "t_admin_id = {$data['t_admin_id']}";
            $user = new TourModel();
            $res = $user->editPass($where, $hashPassword);
            if ($res) {
                return 1;
            } else {
                return 2;
            }
        }
    }

    /***********************************************酒店相关**********************************************/

    /*
     * 酒店列表
     */
    public function hotel()
    {
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }

            $where['a.is_del'] = array('eq', 0);
            $where['is_pass'] = array('eq', 1);
            if (isset($pagedata['tour_name']) && $pagedata['tour_name'] != '') {
                $where['hotel_name'] = array('like', '%' . $pagedata['tour_name'] . '%');
            }
            if (isset($pagedata['tour_tel']) && $pagedata['tour_tel'] != '') {
                $where['hotel_tel'] = array('like', '%' . $pagedata['tour_tel'] . '%');
            }
            if (isset($pagedata['kind_type']) && $pagedata['kind_type'] != '') {
                $where['kind_type'] = array('like', '%' . $pagedata['kind_type'] . '%');
            }
             if (isset($pagedata['examine_time']) && $pagedata['examine_time'] != ''){
                $created_at=explode(' - ', $pagedata['examine_time']); 
                if (isset($created_at) && is_array($created_at)){
                    foreach($created_at as $k=>$v)
                    {
                       $created_at[$k] = strtotime($v);
                    }  
                }
              $where['examine_time']=['between time',$created_at]; 
            }

            //处理好评
            if (isset($pagedata['appraise']) && $pagedata['appraise'] != '') {
                //判断有没有
                $join = explode('~', $pagedata['appraise']);
                if (count($join) == 1) {
                    $where['evaluat_rate'] = $join[0];
                } else if (count($join) == 2) {
                    $where['evaluat_rate'] = ['between', [$join[0], $join[1]]];
                }
            }
            $hotel = new HotelModel();
            $data = $hotel->getTourList($where, $startrow, $pagesize);
            if(count($data)>0){
                foreach ($data as $key => $value) {
                    //1星级酒店 2民宿客栈 3快捷宾馆
                    if($value['kind_type'] == 1)
                     $data[$key]['kind_type'] = '星级酒店';
                    if($value['kind_type'] == 2)
                     $data[$key]['kind_type'] = '民宿客栈';
                    if($value['kind_type'] == 3)
                     $data[$key]['kind_type'] = '快捷宾馆';
                }
            }
            $count = $hotel->getTourCount($where);
            ajax_return(config('code.success_show'), '请求成功', $data, $count);
        } else {
            //查询所有的酒店
            $res1 = Db::name('hotel')->where('is_pass', 1)->where('is_del', 0)->order('hotel_id', 'desc')->select();
            $this->assign('res', $res1);
            foreach ($res1 as $k => $v) {
                $evaluat_rate = model('AppraiseModel')->getAppRate(2, $v['hotel_id']);
                Db::name('hotel')->where('hotel_id', $v['hotel_id'])->setField('evaluat_rate', $evaluat_rate);
            }
            return view();
        }
    }

    /**
     *酒店图片
     */
    public function hotelImage()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $data = Db::name('hotel_img')->field('img_path')->where('hotel_id', $post['id'])->select();

            if ($data) {
                foreach ($data as $key => $v) {
                    $res [] = $v;
                }
                return json(['code' => 0, 'data' => $res]);

            } else {
                return ['code' => 1, 'message' => '暂无图片'];
            }
        }
    }

    /*
     * 查看酒店详情
     */
    public function hotelInfoDetail()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $where = "hotel_id= {$post['hotel_id']}";

            $hotel = new HotelModel();
            $foodInfo = $hotel->getTourList($where, '', '');
            //处理店铺类型
            $type = explode(',', $foodInfo['kind_type']);
            $foodInfo['kind_type'] = [];
            foreach ($type as $k => $v) {
                if ($v == 1) {
                    array_push($foodInfo['kind_type'], '精选酒店');
                }
                if ($v == 2) {
                    array_push($foodInfo['kind_type'], ' 星级酒店');
                }
                if ($v == 3) {
                    array_push($foodInfo['kind_type'], ' 民俗客栈');
                }
                if ($v == 4) {
                    array_push($foodInfo['kind_type'], ' 快捷宾馆');
                }
            }

            if ($foodInfo) {
                ajax_return(config('code.success_show'), '请求成功', $foodInfo);
            } else {
                ajax_return(config('code.error_show'), '请求失败');
            }
        }
    }

    /*
     * 删除操作
     */
    public function delHotel()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $where = "hotel_id = {$post['id']}";
            $hotel = new HotelModel();
            $res = $hotel->hotelDel($where);
            if ($res) {
                ajax_return(0, '删除成功');
            } else {
                ajax_return(1, '删除失败');
            }
        }
    }

    /*
  * 添加景点入住页面展示
  */
    public function hotelAdd()
    {
        return $this->fetch();
    }

    /*
     * 添加景点入住ajax
     */
    public function addHotel()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $post = $post['data'];

            if (!$post['guide_card_img1']) {
                return ['code' => 1, 'message' => '请上传身份证正面照片'];
                exit;
            }
            if (!$post['guide_card_img2']) {
                return ['code' => 1, 'message' => '请上传身份证反面照片'];
            }
            if (!$post['guide_pic']) {
                return ['code' => 1, 'message' => '上传营业执照'];
            }

            if (!$post['live_action']) {
                return ['code' => 1, 'message' => '上传景点实景图'];
            }
            if(!$post['m_image']){
                return ['code' => 1, 'message' => '请上传酒店门头照'];
            }
            if(!$post['b_image']){
                return ['code' => 1, 'message' => '请上传酒店吧台照'];
            }
            //认证成功，更新客户表
            $post['is_pass'] = 1;
            $post['app_time'] = time();
            $post['examine_time'] = time();
            $post['last_settlement_date'] = date('Y-m-d',time());
            //处理图片
            $post['fs_lagal'] = $post['guide_card_img1'] . ',' . $post['guide_card_img2'];
            $post['business_license'] = $post['guide_pic'];
            $post['address'] = $post['dizhi'];
            $post['city'] = $post['province'] . $post['city'] . $post['county'];
            //处理经纬度
            $post['longitude'] = getDoc($post['city'].$post['address'])[0];
            $post['latitude'] = getDoc($post['city'].$post['address'])[1];
            unset($post['file']);
            unset($post['province']);
            unset($post['county']);
            unset($post['dizhi']);
            unset($post['guide_card_img1']);
            unset($post['guide_card_img2']);
            unset($post['guide_pic']);
            //先查询是否入住过了
            $res = Db::name('hotel')->where('send_tel', $post['send_tel'])
                ->find();
            if ($res) {
                return ['code' => 1, 'message' => '已经入住过了'];
            }
            $admin = Db::name('hotel_admin')->where('h_admin_name',$post['send_tel'])->find();
            if ($admin) {
                return ['code' => 3, 'message' => '该手机号已经成为商户'];
            }
            //生成账号和密码发送给商户
            $password = substr(base_convert(md5(uniqid(md5(microtime(true)), true)), 16, 10), 0, 6);
            //加密方式
            $hasher = new \PasswordHash(8, false);
            //将密码进行哈希，结果是一个长度为60个字符的字符串
            $hashPassword = $hasher->HashPassword($password);

            $view = new HotelModel();
            $res = $view->insertView($post);
            //插入到商户表
            $data = [
                'h_admin_name' => $post['send_tel'],
                'password' => $hashPassword,
                'hotel_id' => $res,
                'add_time' => date('Y-m-d H:i:s')
            ];
            $fasong = [
                '1' => $post['hotel_name'],
                '2' => $post['send_tel'],
                '3' => $password,
                '5' => $post['hotel_name']
            ];
            if ($res) {
                sendJsmsMessage($post['send_tel'], 176238, $fasong);
                $send = Db::name('hotel_admin')->insert($data);
                if ($send) {
                    ajax_return(config('code.success_show'), '添加成功');
                }
            } else {
                ajax_return(2, '添加失败');
            }

        }
    }

    /*
     * 修改酒店账号密码
     */
    public function editHotelPass()
    {
        if (Request::instance()->isAjax()) {
            $data = Request::instance()->post();
            $data = $data['data'];
            //加密方式
            $hasher = new \PasswordHash(8, false);
            //将密码进行哈希，结果是一个长度为60个字符的字符串
            $hashPassword = $hasher->HashPassword($data['password']);
            $where = "h_admin_id = {$data['h_admin_id']}";
            $hotel = new HotelModel();
            $res = $hotel->editPass($where, $hashPassword);
            if ($res) {
                return 1;
            } else {
                return 2;
            }
        }
    }

    /*
    * 修改酒店账号手机号
    */
    public function editHotelTel()
    {
        if (Request::instance()->isAjax()) {
            $data = Request::instance()->post();
            $data = $data['data'];
            //先去查询原始数据
            $re = Db::name('hotel')->where('hotel_id', $data['h_admin_id'])->find();
            if ($re) {
                //判断是否有手机号存在
                $phone = Db::name('hotel')->where('send_tel', $data['phone'])->find();
                if ($phone) {
                    return 3;
                }
                $cun = Db::name('hotel_admin')->where('h_admin_name', $data['phone'])->find();
                if ($cun) {
                    return 3;
                }
            }
            $res = Db::name('hotel')->where('hotel_id', $data['h_admin_id'])->setField('send_tel', $data['phone']);
            if ($res) {
                if (!empty($re)) {
                     //查询手机号
                    $user = Db::name('user')->where('phone',$data['phone'])->find();
                    if($user){
                        Db::name('user')->where('phone',$data['phone'])->setField('hotel_id', $data['h_admin_id']);
                    }
                    //修改后台的
                    $admin = Db::name('hotel_admin')->where('h_admin_name', $re['send_tel'])->setField('h_admin_name', $data['phone']);
                    if ($admin) {
                        $fasong = [
                            '1' => $data['phone'],
                            '2' => $data['hotel_name']
                        ];
                        sendJsmsMessage($data['phone'], 176411, $fasong);
                    }
                }
                return 1;
            } else {
                return 2;
            }
        }
    }

    /*
     * 修改景点账号手机号
     */
    public function editTourTel()
    {
        if (Request::instance()->isAjax()) {
            $data = Request::instance()->post();
            $data = $data['data'];
            //先去查询原始数据
            $re = Db::name('tours')->where('tour_id', $data['h_admin_id'])->find();
            if ($re) {

                //判断是否有手机号存在
                $phone = Db::name('tours')->where('send_tel', $data['phone'])->find();
                if ($phone) {
                    return 3;
                }
                $cun = Db::name('tour_admin')->where('t_admin_tel', $data['phone'])->find();
                if ($cun) {
                    return 3;
                }
            }
            $res = Db::name('tours')->where('tour_id', $data['h_admin_id'])->setField('send_tel', $data['phone']);
            if ($res) {
                    //查询手机号
                    $user = Db::name('user')->where('phone',$data['phone'])->find();
                    if($user){
                        Db::name('user')->where('phone',$data['phone'])->setField('tour_id', $data['h_admin_id']);
                    }
                    //修改后台的
                    $admin = Db::name('tour_admin')->where('t_admin_tel', $re['send_tel'])->setField('t_admin_tel', $data['phone']);
                    if ($admin) {
                        $fasong = [
                            '1' => $data['phone'],
                            '2' => $data['hotel_name']
                        ];
                        sendJsmsMessage($data['phone'], 176411, $fasong);
                    }
                

                return 1;
            } else {
                return 2;
            }
        }
    }


    /*
        * 修改景点账号手机号
        */
    public function editTeamTel()
    {
        if (Request::instance()->isAjax()) {
            $data = Request::instance()->post();
            $data = $data['data'];
            //先去查询原始数据
            $re = Db::name('team')->where('team_id', $data['h_admin_id'])->find();
            if ($re) {

                //判断是否有手机号存在
                $phone = Db::name('team')->where('send_tel', $data['phone'])->find();
                if ($phone) {
                    return 3;
                }
                $cun = Db::name('team_admin')->where('t_admin_tel', $data['phone'])->find();
                if ($cun) {
                    return 3;
                }
            }
            $res = Db::name('team')->where('team_id', $data['h_admin_id'])->setField('send_tel', $data['phone']);
            if ($res) {
                if (!empty($re)) {
                     //查询手机号
                    $user = Db::name('user')->where('phone',$data['phone'])->find();
                    if($user){
                        Db::name('user')->where('phone',$data['phone'])->setField('team_id', $data['h_admin_id']);
                    }
                    //修改后台的
                    $admin = Db::name('team_admin')->where('t_admin_tel', $re['send_tel'])->setField('t_admin_tel', $data['phone']);
                    if ($admin) {
                        $fasong = [
                            '1' => $data['phone'],
                            '2' => $data['hotel_name']
                        ];
                        sendJsmsMessage($data['phone'], 176411, $fasong);
                    }
                }

                return 1;
            } else {
                return 2;
            }
        }
    }


    /*
     * 修改
     */
    public function editHotel()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $post = $post['data'];
            if (empty($post['hotel_name']) || empty($post['hotel_tel'])) {
                ajax_return(1, '内容不能为空');
            }
            $where = "hotel_id = {$post['hotel_id']}";

            $hotel = new HotelModel();
            $data = [
                'hotel_name' => $post['hotel_name'],
                'hotel_tel' => $post['hotel_tel'],
                //'send_tel' =>$post['send_tel'],
                'city' => $post['province'] . $post['city'] . $post['district'],
                'address' => $post['address'],
                'kind_type' => $post['kind_type'],
                'hotel_tag' => $post['hotel_tag'],
                'hotel_covers' => $post['hotel_covers'],
                'commission_rate' => $post['commission_rate']
            ];
            //先去查询原始数据
            // $re = Db::name('hotel')->where('hotel_id',$post['hotel_id'])->find();
            // if($re){
            //     //判断是否有手机号存在
            //     $phone = Db::name('hotel')->where('send_tel',$post['send_tel'])->find();
            //     if($phone){
            //         ajax_return(3, '手机号已经存在');
            //         exit;
            //     }
            //     $cun =  Db::name('hotel_admin')->where('h_admin_name',$post['send_tel'])->find();
            //     if($cun){
            //         ajax_return(3, '手机号已经存在');
            //         exit;
            //     }
            // }

            $res = $hotel->edit($where, $data);
            if ($res) {
                // if(!empty($re)){
                //     //修改后台的
                //     $admin = Db::name('hotel_admin')->where('h_admin_name',$re['send_tel'])->setField('h_admin_name',$post['send_tel']);
                //     if($admin){
                //         $fasong = [
                //             '1' => $post['send_tel'],
                //             '2'=>$post['hotel_name']
                //         ];
                //         sendJsmsMessage($post['send_tel'], 176411, $fasong);
                //     }
                // }
                $add = $post['province'] . $post['city'] . $post['district'] . $post['address'];
                //添加经纬度

                $data = [
                    'longitude' => getDoc($add)[0],
                    'latitude' => getDoc($add)[1]
                ];
                Db::name('hotel')->where('hotel_id', $post['hotel_id'])->update($data);

                ajax_return(0, '修改成功');
            } else {
                ajax_return(2, '修改失败');
            }
        } else {
            //查询需要的数据
            $hotel_id = input('id', '', 'trim');
            $where = "hotel_id = {$hotel_id}";
            $filed = 'hotel_name,commission_rate,hotel_tel,address,send_tel,kind_type,hotel_tag,hotel_covers';
            $guide = new HotelModel();
            $res = $guide->getTourList($where, '', '', $filed);
            $this->assign('res', $res);
            $this->assign('hotel_id', $hotel_id);
            return $this->fetch();
        }
    }

    /****************************************************旅行社********************************************/
    public function team()
    {
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }

            $where['a.is_del'] = array('eq', 0);
            if (isset($pagedata['team_name']) && $pagedata['team_name'] != '') {
                $where['team_name'] = array('like', '%' . $pagedata['team_name'] . '%');
            }
            if (isset($pagedata['team_phone']) && $pagedata['team_phone'] != '') {
                $where['team_phone'] = array('like', '%' . $pagedata['team_phone'] . '%');
            }
            if (isset($pagedata['examine_time']) && $pagedata['examine_time'] != ''){
                $created_at=explode(' - ', $pagedata['examine_time']); 
                if (isset($created_at) && is_array($created_at)){
                    foreach($created_at as $k=>$v)
                    {
                       $created_at[$k] = strtotime($v);
                    }  
                }
              $where['examine_time']=['between time',$created_at]; 
            }
            $team = new TeamModel();
            $data = $team->getTeamList($where, $startrow, $pagesize);
            $count = $team->getTeamCount($where);
            ajax_return(config('code.success_show'), '请求成功', $data, $count);
        } else {
            $res = Db::name('team')->alias('a')
                ->field('a.*,b.t_admin_id,b.t_admin_tel,b.pid,b.add_time')
                ->join('team_admin b', 'a.team_id=b.team_id', "LEFT")
                ->where('is_pass', 1)
                ->where('a.is_del', 0)
                ->where('b.pid', 0)
                ->select();
            $this->assign('res', $res);
            return view();
        }
    }

    /*
* 添加景点入住页面展示
*/
    public function teamAdd()
    {
        return $this->fetch();
    }

    /*
     * 添加景点入住ajax
     */
    public function addTeam()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $post = $post['data'];

            if (!$post['guide_card_img1']) {
                return ['code' => 1, 'message' => '请上传身份证正面照片'];
                exit;
            }
            if (!$post['guide_card_img2']) {
                return ['code' => 1, 'message' => '请上传身份证反面照片'];
            }
            if (!$post['team_license_image']) {
                return ['code' => 1, 'message' => '上传营业执照'];
            }

            if (!$post['team_image']) {
                return ['code' => 1, 'message' => '上传旅行社实景图'];
            }
            if(!$post['m_image']){
                return ['code' => 1, 'message' => '请上传旅行社门头照'];
            }
            if(!$post['b_image']){
                return ['code' => 1, 'message' => '请上传旅行社吧台照'];
            }

            //认证成功，更新客户表
            $post['is_pass'] = 1;
            $post['app_time'] = time();
            $post['examine_time'] = time(); 
            $post['last_settlement_date'] = date('Y-m-d',time());
            //处理图片
            $post['team_legel_image'] = $post['guide_card_img1'] . ',' . $post['guide_card_img2'];
            $post['team_address'] = $post['dizhi'];
            $post['city'] = $post['province'] . $post['city'] . $post['county'];
            //处理经纬度
            $post['longitude'] = getDoc($post['city'].$post['team_address'])[0];
            $post['latitude'] = getDoc($post['city'].$post['team_address'])[1];
            unset($post['file']);
            unset($post['province']);
            unset($post['county']);
            unset($post['dizhi']);
            unset($post['guide_card_img1']);
            unset($post['guide_card_img2']);
            //先查询是否入住过了
            $res = Db::name('team')->where('send_tel', $post['send_tel'])
                ->find();
            if ($res) {
                return ['code' => 3, 'message' => '已经申请入住'];
            }
            $admin = Db::name('team_admin')->where('t_admin_tel',$post['send_tel'])->find();
            if($admin){
                return ['code' => 3, 'message' => '该手机号已经成为商户'];
            }
            //生成账号和密码发送给商户
            $password = substr(base_convert(md5(uniqid(md5(microtime(true)), true)), 16, 10), 0, 6);
            //加密方式
            $hasher = new \PasswordHash(8, false);
            //将密码进行哈希，结果是一个长度为60个字符的字符串
            $hashPassword = $hasher->HashPassword($password);
            $res = Db::name('team')->insertGetId($post);
            //插入到商户表
            $data = [
                't_admin_tel' => $post['send_tel'],
                'password' => $hashPassword,
                'team_id' => $res,
                'add_time' => date('Y-m-d H:i:s')
            ];
            $fasong = [
                '1' => $post['team_name'],
                '2' => $post['send_tel'],
                '3' => $password,
                '5' => $post['team_name']
            ];
            if ($res) {
                sendJsmsMessage($post['send_tel'], 176239, $fasong);
                $send = Db::name('team_admin')->insert($data);
                if ($send) {
                    ajax_return(config('code.success_show'), '添加成功');
                }
            } else {
                ajax_return(2, '添加失败');
            }

        }
    }

    /**
     * 旅行社图片
     */
    public function teamImage()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $data = Db::name('team_img')->field('img_path')->where('id', $post['id'])->select();

            if ($data) {
                foreach ($data as $key => $v) {
                    $res [] = $v;
                }
                return json(['code' => 0, 'data' => $res]);

            } else {
                return ['code' => 1, 'message' => '暂无图片'];
            }
        }
    }

    /*
     * 详细信息
     */
    public function teamInfoDetail()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $where = "team_id= {$post['team_id']}";

            $team = new TeamModel();
            $foodInfo = $team->getTeamList($where, '', '');

            if ($foodInfo) {
                ajax_return(config('code.success_show'), '请求成功', $foodInfo);
            } else {
                ajax_return(config('code.error_show'), '请求失败');
            }
        }
    }

    /*
     * 修改商户密码
     */
    public function editTeamPassword()
    {
        if (Request::instance()->isAjax()) {
            $data = Request::instance()->post();
            $data = $data['data'];
            //加密方式
            $hasher = new \PasswordHash(8, false);
            //将密码进行哈希，结果是一个长度为60个字符的字符串
            $hashPassword = $hasher->HashPassword($data['password']);
            $where = "t_admin_id = {$data['t_admin_id']}";
            $team = new TeamModel();
            $res = $team->editPass($where, $hashPassword);
            if ($res) {
                return 1;
            } else {
                return 2;
            }
        }
    }

    /*
     * 删除 为删除
     */
    public function delTeam()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $where = "team_id = {$post['id']}";
            $team = new TeamModel();
            $res = $team->teamDel($where);
            if ($res) {
                ajax_return(0, '删除成功');
            } else {
                ajax_return(1, '删除失败');
            }
        }
    }

    /*
     * 修改
     */
    public function editTeam()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $post = $post['data'];
            if (empty($post['team_name']) || empty($post['team_phone'])) {
                ajax_return(1, '内容不能为空');
            }
            $where = "team_id = {$post['team_id']}";
            $data = [
                'team_name' => $post['team_name'],
                'team_phone' => $post['team_phone'],
                // 'send_tel'=>$post['send_tel'],
                'city' => $post['province'] . $post['city'] . $post['district'],
                'team_address' => $post['team_address'],
                'team_start_time' => $post['team_start_time'],
                'team_end_time' => $post['team_end_time'],
            ];
            // //先去查询原始数据
            // $re = Db::name('team')->where('team_id',$post['team_id'])->find();
            // if($re){
            //     //判断是否有手机号存在
            //     $phone = Db::name('team')->where('send_tel',$post['send_tel'])->find();
            //     if($phone){
            //         ajax_return(3, '手机号已经存在');
            //         exit;
            //     }
            //     $cun =  Db::name('team_admin')->where('t_admin_tel',$post['send_tel'])->find();
            //     if($cun){
            //         ajax_return(3, '手机号已经存在');
            //         exit;
            //     }
            // }
            $team = new TeamModel();
            $res = $team->edit($where, $data);
            if ($res) {
                // if(!empty($re)){
                //     //修改后台的
                //     $admin = Db::name('team_admin')->where('t_admin_tel',$re['send_tel'])->setField('t_admin_tel',$post['send_tel']);
                //     if($admin){
                //         $fasong = [
                //             '1' => $post['send_tel'],
                //             '2'=>$post['team_name']
                //         ];
                //         sendJsmsMessage($post['send_tel'], 176411, $fasong);
                //     }
                // }
                $add = $post['province'] . $post['city'] . $post['district'] . $post['team_address'];
                //添加经纬度

                $data = [
                    'longitude' => getDoc($add)[0],
                    'latitude' => getDoc($add)[1]
                ];
                Db::name('team')->where('team_id', $post['team_id'])->update($data);
                ajax_return(0, '修改成功');
            } else {
                ajax_return(2, '修改失败');
            }
        } else {
            //查询需要的数据
            $team_id = input('id', '', 'trim');
            $where = "team_id = {$team_id}";
            $filed = 'team_name,commission_rate,team_phone,team_address,send_tel,city,team_start_time,team_end_time';
            $team = new TeamModel();
            $res = $team->getTeamList($where, '', '', $filed);
            $this->assign('res', $res);
            $this->assign('team_id', $team_id);
            return $this->fetch();
        }
    }


    /*
     * 景点是否推荐
     */
    public function isViewTj()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $res = Db::name('tours')->where('tour_id', $post['id'])->setField('is_tuijian', $post['is_tuijian']);

            if ($res !== false) {
                return ['code' => 1];
            } else {
                return ['code' => 2];
            }
        }
    }

    public function isHotelTj()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $res = Db::name('hotel')->where('hotel_id', $post['id'])->setField('is_tuijian', $post['is_tuijian']);

            if ($res !== false) {
                return ['code' => 1];
            } else {
                return ['code' => 2];
            }
        }
    }

    public function isStickTj()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $up['is_stick'] = $post['is_stick'];
            $up['stick_time'] = date("Y-m-d H:i:s", time());
            $res = Db::name('hotel')->where('hotel_id', $post['id'])->update($up);

            if ($res !== false) {
                return ['code' => 1];
            } else {
                return ['code' => 2];
            }
        }
    }

    public function isFoodStickTj()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $up['is_stick'] = $post['is_stick'];
            $up['stick_time'] = date("Y-m-d H:i:s", time());
            $res = Db::name('food_store')->where('food_store_id', $post['id'])->update($up);

            if ($res !== false) {
                return ['code' => 1];
            } else {
                return ['code' => 2];
            }
        }
    }

    public function isViewStickTj()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $up['is_stick'] = $post['is_stick'];
            $up['stick_time'] = date("Y-m-d H:i:s", time());
            $res = Db::name('tours')->where('tour_id', $post['id'])->update($up);

            if ($res !== false) {
                return ['code' => 1];
            } else {
                return ['code' => 2];
            }
        }
    }


    public function isFoodTj()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $res = Db::name('food_store')->where('food_store_id', $post['id'])->setField('is_tuijian', $post['is_tuijian']);

            if ($res !== false) {
                return ['code' => 1];
            } else {
                return ['code' => 2];
            }
        }
    }

    /**
     * 双击修改单元格*
     */
    public function editViewSort()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $res = Db::name('tours')->where('tour_id', $post['id'])->setField('sort', $post['sort']);
            if ($res !== false) {
                return ['code' => 0];
            } else {
                return ['code' => '修改失败'];
            }
        }
    }

    public function editHotelSort()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $res = Db::name('hotel')->where('hotel_id', $post['id'])->setField('sort', $post['sort']);
            if ($res !== false) {
                return ['code' => 0];
            } else {
                return ['code' => '修改失败'];
            }
        }
    }

    public function editFoodSort()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $res = Db::name('food_store')->where('food_store_id', $post['id'])->setField('sort', $post['sort']);
            if ($res !== false) {
                return ['code' => 0];
            } else {
                return ['code' => '修改失败'];
            }
        }
    }

    //是否上架
    public function isTourShow()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            //查询该商户是否有商品 图片数是否大于3
            $img = Db::name('hotel_img')->where('hotel_id', $post['id'])->count();
            $room = Db::name('hotel_room')->where('hotel_id', $post['id'])->select();
//            if ($post['is_display'] == 1) {
//                $res = Db::name('hotel')->where('hotel_id', $post['id'])->setField('is_display', $post['is_display']);
//                if ($res !== false) {
//                    return ['code' => 1];
//                } else {
//                    return ['code' => 3, 'msg' => '修改失败'];
//                }
//            } else {
                if ($img < 3 || empty($room)) {
                    return ['code' => 2, 'msg' => '商品或酒店相册没有完善'];
                } else {
                    $res = Db::name('hotel')->where('hotel_id', $post['id'])->setField('is_display', $post['is_display']);
                    if ($res !== false) {
                        return ['code' => 1];
                    } else {
                        return ['code' => 3, 'msg' => '修改失败'];
                    }
                }
//            }


        }
    }
    //查看有无核销订单或者待确认的订单
    public function isOrder()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            //查询酒店订单表
            $hotel_order = Db::name('hotel_order')->field('order_id,order_sn')->where('hotel_id',$post['id'])
                ->where('order_status',1)->whereOr('order_status',6)->select();
            if(empty($hotel_order)){
                return ['code'=>1];
            }else{
                //获取待确认的总单数和总钱数
                $q_sum = Db::name('hotel_order')->where('hotel_id',$post['id'])->where('order_status',6)
                    ->where('is_del',0)->count();
                $q_money = Db::name('hotel_order')->where('hotel_id',$post['id'])->where('order_status',6)
                    ->where('is_del',0)->sum('order_price');
                //待核销的总单数和总钱数
                $h_sum = Db::name('hotel_order')->where('hotel_id',$post['id'])->where('order_status',1)
                    ->where('is_del',0)->count();
                $h_money = Db::name('hotel_order')->where('hotel_id',$post['id'])->where('order_status',1)
                    ->where('is_del',0)->sum('order_price');

                $all_money = $q_money+$h_money;
                return ['code'=>2,'q_sum'=>$q_sum,'all_money'=>$all_money,'h_sun'=>$h_sum,'id'=>$post['id']];

            }

        }
    }
    //景点查看有无核销的订单
    public function isTourOrder()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            //查询酒店订单表
            $tour_order = Db::name('tour_order')->field('order_id,order_sn')->where('tour_id',$post['id'])
                ->where('order_status',1)->select();
            if(empty($tour_order)){
                return ['code'=>1];
            }else{

                //待核销的总单数和总钱数
                $h_sum = Db::name('tour_order')->where('tour_id',$post['id'])->where('order_status',1)
                    ->where('is_del',0)->count();
                $h_money = Db::name('tour_order')->where('tour_id',$post['id'])->where('order_status',1)
                    ->where('is_del',0)->sum('order_price');

                return ['code'=>2,'all_money'=>$h_money,'h_sun'=>$h_sum,'id'=>$post['id']];

            }

        }
    }
    //查看旅行社有无核销订单
    public function isOrderTeam()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            //查询酒店订单表
            $team_order = Db::name('gtour_order')->field('order_id,order_sn')->where('team_id',$post['id'])
                ->where('order_status',1)->select();
            if(empty($team_order)){
                return ['code'=>1];
            }else{

                //待核销的总单数和总钱数
                $h_sum = Db::name('gtour_order')->where('team_id',$post['id'])->where('order_status',1)
                    ->where('is_del',0)->count();
                $h_money = Db::name('gtour_order')->where('team_id',$post['id'])->where('order_status',1)
                    ->where('is_del',0)->sum('order_price');

                return ['code'=>2,'all_money'=>$h_money,'h_sun'=>$h_sum,'id'=>$post['id']];

            }

        }
    }

    //没有订单下架
    public function isXj()
    {
        if(Request::instance()->isAjax()){
            $post = Request::instance()->post();
            $res = Db::name('hotel')->where('hotel_id', $post['id'])->setField('is_display', $post['is_display']);
            if ($res !== false) {
                return ['code' => 1];
            } else {
                return ['code' => 3, 'msg' => '下架失败'];
            }
        }
    }
    //景点下架
    public function  isXjTour()
    {
        if(Request::instance()->isAjax()){
            $post = Request::instance()->post();
            $res = Db::name('tours')->where('tour_id', $post['id'])->setField('is_display', $post['is_display']);
            if ($res !== false) {
                return ['code' => 1];
            } else {
                return ['code' => 3, 'msg' => '下架失败'];
            }
        }
    }
    //旅行社下架
    public function isXjTeam()
    {
        if(Request::instance()->isAjax()){
            $post = Request::instance()->post();
            //查询旗下有没有商品
            $q = Db::name('group_tour')->where('team_id',$post['id'])->where('is_del',0)->select();
            if(!empty($q)){
                $res = Db::name('team')->where('team_id', $post['id'])->setField('is_display', $post['is_display']);
                //修改旗下的商品
                $goods = Db::name('group_tour')->where('team_id',$post['id'])->setField('is_display',$post['is_display']);
                if ($res !== false && !empty($goods)) {
                    return ['code' => 1];
                } else {
                    return ['code' => 3, 'msg' => '修改失败'];
                }
            }else{
                $res = Db::name('team')->where('team_id', $post['id'])->setField('is_display', $post['is_display']);
                if ($res !== false) {
                    return ['code' => 1];
                } else {
                    return ['code' => 3, 'msg' => '修改失败'];
                }
            }
        }
    }
    //确认后退款操作
    public function isRefund()
    {
        if(Request::instance()->isAjax()){
            $post = Request::instance()->post();
            //处理未付款的订单
            Db::name('hotel_order')->where('hotel_id',$post['id'])
                ->where('order_status',0)->setField('order_status',2);

            $hotel = Db::name('hotel_order')->field('order_sn,uid,order_price,pay_type')->where('hotel_id',$post['id'])
                ->where('order_status',1)->whereOr('order_status',6)->select();
            if(!empty($hotel)){
                foreach ($hotel as $k=>$value){

                    Db::startTrans();
                    try{
                        $jobHandlerClassName  = 'app\api\job\Miss';
                        // 2.当前任务归属的队列名称，如果为新队列，会自动创建
                        $jobQueueName  	  = "miss";
                        // 3.当前任务所需的业务数据 . 不能为 resource 类型，其他类型最终将转化为json形式的字符串
                        //   ( jobData 为对象时，需要在先在此处手动序列化，否则只存储其public属性的键值对)
                        $jobData       	  = [
                            'uid'=>$value['uid'],
                            'order_sn'=>$value['order_sn'],
                            'order_price'=>$value['order_price'],
                            'pay_type' => $value['pay_type'],
                        ] ;
                        // 4.将该任务推送到消息队列，等待对应的消费者去执行
                        $isPushed = Queue::push( $jobHandlerClassName , $jobData , $jobQueueName );
                        // database 驱动时，返回值为 1|false  ;   redis 驱动时，返回值为 随机字符串|false
                        if( $isPushed !== false ){
                            Db::commit();
                        }else{
                            Db::rollback();
                        }
                    }catch (\Exception $e) {
                        Db::rollback();
                    }
                }
            }
            //修改状态
            $display = Db::name('hotel')->where('hotel_id',$post['id'])->setField('is_display',1);
            if($display){
                return '1';
            }else{
                return '2';
            }
        }
    }

    //景点确认后退款
    public function isRefundTour()
    {
        if(Request::instance()->isAjax()){
            $post = Request::instance()->post();
            //处理未付款的订单
            Db::name('tour_order')->where('tour_id',$post['id'])
                ->where('order_status',0)->setField('order_status',2);

            $hotel = Db::name('tour_order')->field('order_sn,user_id,order_price,pay_type')->where('tour_id',$post['id'])
                ->where('order_status',1)->select();
            if(!empty($hotel)){
                foreach ($hotel as $k=>$value){

                    Db::startTrans();
                    try{
                        $jobHandlerClassName  = 'app\api\job\Tour';
                        // 2.当前任务归属的队列名称，如果为新队列，会自动创建
                        $jobQueueName  	  = "tour";
                        // 3.当前任务所需的业务数据 . 不能为 resource 类型，其他类型最终将转化为json形式的字符串
                        //   ( jobData 为对象时，需要在先在此处手动序列化，否则只存储其public属性的键值对)
                        $jobData       	  = [
                            'uid'=>$value['user_id'],
                            'order_sn'=>$value['order_sn'],
                            'order_price'=>$value['order_price'],
                            'pay_type' => $value['pay_type'],
                        ] ;
                        // 4.将该任务推送到消息队列，等待对应的消费者去执行
                        $isPushed = Queue::push( $jobHandlerClassName , $jobData , $jobQueueName );
                        // database 驱动时，返回值为 1|false  ;   redis 驱动时，返回值为 随机字符串|false
                        if( $isPushed !== false ){
                            Db::commit();
                        }else{
                            Db::rollback();
                        }
                    }catch (\Exception $e) {
                        Db::rollback();
                    }
                }
            }
            //修改状态
            $display = Db::name('tours')->where('tour_id',$post['id'])->setField('is_display',1);
            if($display){
                return '1';
            }else{
                return '2';
            }
        }
    }

    //旅行社确认后退款
    public function isRefundTeam()
    {
        if(Request::instance()->isAjax()){
            $post = Request::instance()->post();
            //处理未付款的订单
            Db::name('gtour_order')->where('team_id',$post['id'])
                ->where('order_status',0)->setField('order_status',2);

            $hotel = Db::name('gtour_order')->field('order_sn,user_id,order_price,pay_type')->where('team_id',$post['id'])
                ->where('order_status',1)->select();
            if(!empty($hotel)){
                foreach ($hotel as $k=>$value){

                    Db::startTrans();
                    try{
                        $jobHandlerClassName  = 'app\api\job\Team';
                        // 2.当前任务归属的队列名称，如果为新队列，会自动创建
                        $jobQueueName  	  = "team";
                        // 3.当前任务所需的业务数据 . 不能为 resource 类型，其他类型最终将转化为json形式的字符串
                        //   ( jobData 为对象时，需要在先在此处手动序列化，否则只存储其public属性的键值对)
                        $jobData       	  = [
                            'uid'=>$value['user_id'],
                            'order_sn'=>$value['order_sn'],
                            'order_price'=>$value['order_price'],
                            'pay_type' => $value['pay_type'],
                        ] ;
                        // 4.将该任务推送到消息队列，等待对应的消费者去执行
                        $isPushed = Queue::push( $jobHandlerClassName , $jobData , $jobQueueName );
                        // database 驱动时，返回值为 1|false  ;   redis 驱动时，返回值为 随机字符串|false
                        if( $isPushed !== false ){
                            Db::commit();
                        }else{
                            Db::rollback();
                        }
                    }catch (\Exception $e) {
                        Db::rollback();
                    }
                }
            }
            //修改状态
            $q = Db::name('group_tour')->where('team_id',$post['id'])->where('is_del',0)->select();
            if(!empty($q)){
                $res = Db::name('team')->where('team_id', $post['id'])->setField('is_display', 1);
                //修改旗下的商品
                $goods = Db::name('group_tour')->where('team_id',$post['id'])->setField('is_display',1);
                if ($res !== false && !empty($goods)) {
                    return '1';
                } else {
                    return '2';
                }
            }else{
                $res = Db::name('team')->where('team_id', $post['id'])->setField('is_display', 1);
                if ($res !== false) {
                    return '1';
                } else {
                    return '2';
                }
            }
        }
    }

    //酒店上架
    public function isHotelShow()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            //查询该商户是否有商品 图片数是否大于3
            $img = Db::name('tour_img')->where('tour_id', $post['id'])->count();
            $room = Db::name('tour_ticket')->where('tour_id', $post['id'])->select();
//            if ($post['is_display'] == 1) {
//                $res = Db::name('tours')->where('tour_id', $post['id'])->setField('is_display', $post['is_display']);
//                if ($res !== false) {
//                    return ['code' => 1];
//                } else {
//                    return ['code' => 3, 'msg' => '修改失败'];
//                }
//            } else {
                if ($img < 3 || empty($room)) {
                    return ['code' => 2, 'msg' => '商品或景点相册没有完善'];
                } else {
                    $res = Db::name('tours')->where('tour_id', $post['id'])->setField('is_display', $post['is_display']);
                    if ($res !== false) {
                        return ['code' => 1];
                    } else {
                        return ['code' => 3, 'msg' => '修改失败'];
                    }
                }
//            }


        }
    }
    //旅行社上架
    public function isTeamShow()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            //查询旗下有没有商品
            $q = Db::name('group_tour')->where('team_id',$post['id'])->where('is_del',0)->select();
            if(!empty($q)){
                $res = Db::name('team')->where('team_id', $post['id'])->setField('is_display', $post['is_display']);
                //修改旗下的商品
                $goods = Db::name('group_tour')->where('team_id',$post['id'])->setField('is_display',$post['is_display']);
                if ($res !== false && !empty($goods)) {
                    return ['code' => 1];
                } else {
                    return ['code' => 3, 'msg' => '修改失败'];
                }
            }else{
                $res = Db::name('team')->where('team_id', $post['id'])->setField('is_display', $post['is_display']);
                if ($res !== false) {
                    return ['code' => 1];
                } else {
                    return ['code' => 3, 'msg' => '修改失败'];
                }
            }

        }
    }
    public function upload_hotel()
    {
        $id = input('id', '', 'intval');
        Session::set('upload_hotel_id', $id);
        return view();
    }

    public function uploadsImg()
    {

        $upload = parent::uploadOneFileToQiniu('lv/hotel/album/');
        $id = input('id', '', 'intval');
        if ($upload['code'] == 0) {

            $addData = [
                'hotel_id' => $id,
                'img_path' => $upload['data'],
                'create_time' => date("Y-m-d H:i:s"),
            ];

            $res = AlbumModel::add($addData);

            if ($res !== false) {
                return ['code' => 0, 'msg' => '上传成功'];
            } else {
                return ['code' => 1, 'msg' => '上传失败'];
            }

        } else {

            return $upload;

        }


    }

    public function uploadTourImg()
    {
        $upload = parent::uploadOneFileToQiniu('lv/hotel/album/');
        $id = input('id', '', 'intval');
        if ($upload['code'] == 0) {

            $addData = [
                'tour_id' => $id,
                'img_path' => $upload['data'],
                'create_time' => date("Y-m-d H:i:s"),
            ];

            $res = Db::name('tour_img')->insertGetId($addData);

            if ($res !== false) {
                return ['code' => 0, 'msg' => '上传成功'];
            } else {
                return ['code' => 1, 'msg' => '上传失败'];
            }

        } else {

            return $upload;

        }
    }

    //美食的上架 下架
    public function isFoodShow()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();

            $res = Db::name('food_store')->where('food_store_id', $post['id'])->setField('is_display', $post['is_display']);
            if ($res !== false) {
                return ['code' => 1];
            } else {
                return ['code' => 3, 'msg' => '下架失败'];
            }

        }
    }

}